package com.fyzk.activity.news;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.fyzk.activity.lottery.basketball.JCLQFragmentActivityI;
import com.fyzk.activity.lottery.jc.football.newJz.FootballFragmentActivityI;
import com.fyzk.base.BaseActivity;
import com.fyzk.utils.LogUtil;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

/**
 * Created by fanxs11 on 17/4/22.
 */
public class NewsDetailActivity extends BaseActivity {
    private TopTitleView topView;
    private WebView webView;
    private Button btnBet;
    private NewsListBean.DataBean info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_newsdetail);
        info = (NewsListBean.DataBean) getIntent().getSerializableExtra("info");

        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setCenterText("新闻详情");
        topView.setOnTitleClickListener(this);

        webView = (WebView) findViewById(R.id.newsdetail_web);
        btnBet = (Button) findViewById(R.id.newsDetailUI_btnBet);
        btnBet.setOnClickListener(this);
        if (!TextUtils.isEmpty(info.getName())) {
            if (info.getName().contains("足球")) {
                btnBet.setVisibility(View.VISIBLE);
                btnBet.setText("去竞彩足球投注");
            } else if (info.getName().contains("篮球")) {
                btnBet.setVisibility(View.VISIBLE);
                btnBet.setText("去竞彩篮球投注");
            } else {
                btnBet.setVisibility(View.GONE);
            }
        } else {
            btnBet.setVisibility(View.GONE);
        }

        webView.loadUrl(info.getDetail_url());
        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSaveFormData(false);
        // 打开javaScript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);

        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                        String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        webView.setWebViewClient(new MyWebViewClient());
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.newsDetailUI_btnBet:
                if ("去竞彩足球投注".equals(btnBet.getText().toString())) {
                    startActivity(new Intent(this, FootballFragmentActivityI.class));
                } else if ("去竞彩篮球投注".equals(btnBet.getText().toString())) {
                    startActivity(new Intent(this, JCLQFragmentActivityI.class));
                }
                break;
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtil.d("shouldOverrideUrlLoading>>>>" + url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            event.getDeviceId();
            return super.shouldOverrideKeyEvent(view, event);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            //在加载完成之后对网页中内容进行隐藏
            //getElementById()
            //getElementsByName()
            //getElementsByTagName()

            //编写 javaScript创建一个方法，将要执行的js代码放到这个方法中
            String javascript = "javascript:function hideOther() {" +
                    "document.getElementsByTagName('header')[0].style.display='none';" +
                    "document.getElementsByClassName('native-scroll')[0].style.top=0;}";

            //创建方法
            view.loadUrl(javascript);

            //加载方法
            view.loadUrl("javascript:hideOther();");
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

}
